package com.android.jay.lib_audio.mediaplayer.core;

import android.media.MediaPlayer;

import java.io.IOException;

/**
 * Created by wzp on 2020/1/12 0012.
 * <p>
 * 描述  ：带状态的mediaplayer
 */
public class CustomMediaPlayer extends MediaPlayer implements MediaPlayer.OnCompletionListener {


    public enum Status {
        IDEL, INITALIZED, STARTED, PAUSE, STOPPTED, COMPLETED;
    }

    private Status mState = Status.IDEL; //当前状态
    private OnCompletionListener mCompleteListener;

    public CustomMediaPlayer() {
        super();
        mState = Status.IDEL;
        super.setOnCompletionListener(this);
    }


    @Override
    public void reset() {
        super.reset();
        mState = Status.IDEL;
    }

    @Override
    public void setDataSource(String path) throws IOException, IllegalArgumentException, IllegalStateException, SecurityException {
        super.setDataSource(path);
        mState = Status.INITALIZED;
    }

    @Override
    public void start() throws IllegalStateException {
        super.start();
        mState = Status.STARTED;
    }

    @Override
    public void pause() throws IllegalStateException {
        super.pause();
        mState = Status.PAUSE;
    }

    @Override
    public void stop() throws IllegalStateException {
        super.stop();
        mState = Status.STOPPTED;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mState = Status.COMPLETED;
    }

    /**
     * @return 获取播放器状态
     **/
    public Status getState() {
        return mState;
    }

    /**
     * @return 是否播放完毕
     **/
    public boolean isComplete() {
        return mState == Status.COMPLETED;
    }


    public void setCompleteListener(OnCompletionListener listener) {
        this.mCompleteListener = listener;
    }


}
